package com.zisuyi.game.sdk.game

import android.animation.LayoutTransition
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.weilele.mvvm.base.helper.appCompatActivity
import com.weilele.mvvm.base.helper.ignoreError
import com.weilele.mvvm.base.helper.ignoreErrorToast
import com.weilele.mvvm.utils.activity.dp2Px
import com.weilele.mvvm.utils.activity.enableDefaultLayoutAnim
import com.weilele.mvvm.utils.activity.navigateTo
import com.weilele.mvvm.widget.BaseLinearLayout
import com.zisuyi.game.sdk.base.BaseFragment

class GameFragment : BaseFragment<ViewBinding>() {
    companion object {
        fun start(appCompatActivity: AppCompatActivity?) {
            appCompatActivity?.navigateTo<GameActivity>()
        }
    }

    val gameEgret by lazy { GameEgret(appCompatActivity!!) }
    override fun getLayoutResOrView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Any {
        return BaseLinearLayout(inflater.context).apply {
            setBackgroundColor(Color.parseColor("#FF8F9983"))
            setPadding(0, dp2Px(24), 0, dp2Px(8))
            layoutTransition = LayoutTransition()
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            orientation = LinearLayout.VERTICAL
            addView(gameEgret.rootFrameLayout, LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                0
            ).apply { weight = 1f })
            //分割线
            addView(
                View(inflater.context),
                LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp2Px(8))
            )
//            ignoreError {
//                gameEgret.gameAd?.getBannerView()?.let {
//                    addView(
//                        it,
//                        LinearLayout.LayoutParams(
//                            LinearLayout.LayoutParams.MATCH_PARENT,
//                            LinearLayout.LayoutParams.WRAP_CONTENT
//                        )
//                    )
//                }
//            }
        }
    }

    override fun onBackPressed(): OnBackPressedCallback? {
        return object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                gameEgret.onBackPressed()
            }
        }
    }
}